<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="80vw" destroy-on-close @closed="$emit('closed')" align-center
			   :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-row :gutter="1">
				<el-col :span="8">
					<el-form-item label="工艺编号" prop="route_code">
						<el-input v-model="form.route_code" placeholder="请输入工艺编号" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="工艺名称" prop="route_name">
						<el-input v-model="form.route_name" placeholder="请输入工艺名称" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="工艺备注" prop="remark">
						<el-input v-model="form.remark" placeholder="请输入工艺备注" clearable type="textarea"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="工序信息" prop="detail">
				<!-- <el-button type="primary" size="small" @click="selectProcess">选择工序</el-button> -->
				<sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="400">
					<el-table-column label="类型" align="center" prop="type">
						<template #default="scope">
							<el-select v-model="scope.row.type" placeholder="请选择" filterable>
								<el-option label="工序" :value="1"></el-option>
								<el-option label="工艺" :value="2"></el-option>
							</el-select>
						</template>
					</el-table-column>
					<el-table-column label="工序/工艺路线" align="center" prop="working_route_id">
						<template #default="scope">
							<el-select v-model="scope.row.working_route_id" placeholder="请选择" filterable v-if="scope.row.type == 1" @change="processChange($event, scope.$index)">
								<el-option v-for="item in processList" :key="item.id" :label="item.working_name" :value="item.id"/>
							</el-select>
							<el-select v-model="scope.row.working_route_id" placeholder="请选择" filterable v-if="scope.row.type == 2">
								<el-option v-for="item in craftList" :key="item.id" :label="item.route_name" :value="item.id"/>
							</el-select>
						</template>
					</el-table-column>
					<el-table-column label="报工配比" align="center" prop="ratio">
						<template #default="scope">
							<el-input v-model="scope.row.ratio" placeholder="请输入报工配比" clearable></el-input>
						</template>
					</el-table-column>
					<el-table-column label="工序要求" align="center" prop="content">
						<template #default="scope">
							<el-input v-model="scope.row.content" placeholder="请输入工序要求" clearable></el-input>
						</template>
					</el-table-column>
					<el-table-column label="图纸" align="center" prop="drawing" width="80">
						<template #default="scope">
							<sc-upload height="50" width="50" v-model="scope.row.drawing"></sc-upload>
						</template>
					</el-table-column>
					<el-table-column label="工序SOP文档" align="center" prop="sop">
						<template #default="scope">
							<sc-upload-file v-model="scope.row.sop" :limit="1">
								<el-button type="primary" icon="el-icon-upload" size="small">选择文件</el-button>
							</sc-upload-file>
						</template>
					</el-table-column>
				</sc-form-table>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
	<select-process v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></select-process>
</template>

<script>
import selectProcess from '../process/selectProcess.vue';

export default {
	components: {selectProcess},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增工艺',
				edit: '编辑工艺',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			dialog: {
				save: false,
			},
			form: {
				route_name: '',
				route_code: "",
				remark: "",
				detail: [],
			},
			rules: {
				route_name: [
					{required: true, message: '请输入工艺名称'}
				],
			},
			addTemplate: {},
			processList:[],
			craftList:[]
		}
	},
	mounted() {
		this.getProcess()
		this.getCraft()
	},
	methods: {
		/**工序选择事件*/
		processChange(e,index) {
			this.processList.map(item => {
				if (this.form.detail[index].working_route_id == item.id) {
					this.form.detail[index].working_route_id = item.id
					this.form.detail[index].content = item.content
					this.form.detail[index].drawing = item.drawing
					this.form.detail[index].sop = item.sop
					this.form.detail[index].ratio = item.ratio
				}
			})
		},
		/**获取工序*/
		async getProcess() {
			var res = await this.$API.goods.process.all.get();
			this.processList = res.data;
		},
		/**获取工序*/
		async getCraft() {
			var res = await this.$API.goods.craft.all.get();
			this.craftList = res.data;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.goods.craft.edit.post(this.form);
					} else {
						res = await this.$API.goods.craft.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
			var res = await this.$API.goods.craft.detail.get({id: data.id});
			Object.assign(this.form, res.data)
		},
	}
}
</script>

<style>
.el-input-group__append {
	padding: 0 8px;
}
</style>
